<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/9/7
 * Time: 下午1:42
 */

namespace Home\Controller;


use Think\Controller;

class PassportController extends BaseController
{

    //todo login
    public function login()
    {
        if(IS_AJAX){
            $mobile = I('username');
            $password = I('password');
            if(!trim($mobile)) $this->ajaxReturn(array('status' => 0, 'msg' => '请输入手机号码'));
            if(!trim($password)) $this->ajaxReturn(array('status' => 0, 'msg' => '请输入密码'));
            $password = md5($password);
            $where = array('mobile' => $mobile, 'password' => $password);
            $member = D('member')->where($where)->field('id,username,mobile')->find();
            if(!$member) $this->ajaxReturn(array('status' => 0, 'msg' => '用户名或密码不正确'));
            session('current_member', array('mobile' => $member['mobile'], 'name' => $member['username'], 'uid' => $member['id']));
            $this->ajaxReturn(array('status' => 1, 'msg' => '登录成功'));
        }
        $this->display();
    }

    //todo register
    public function register()
    {
        if(IS_AJAX){

            $username = I('username');
            $password = I('password');
            //账户唯一性检测
            if(strlen($username) < 2 || strlen($username) >12) $this->ajaxReturn(array('status' => 0, 'msg' => '用户名2～12个字符'));
            if(strlen($password) < 6) $this->ajaxReturn(array('status' => 0, 'msg' => '密码至少6位'));
            $mod = D('member');


            if($mod->create())
            {
                $mod->addtime = time();
                $mod->password = md5($password);
                $uid = $mod->add();

//              //亲属信息写入
                $family = $_POST['family'];
                $mod_ = D('member_relation');
                $tels = i_array_column($family, 'tel');
                if($mod_->checkUnique($tels)){
                    $mod->where(array('id'=> $uid))->delete();
                    $this->ajaxReturn(array('status' => 0, 'msg' => '亲属号码已经被使用'));
                }
                foreach($family as $key => $val){
                    $family[$key]['uid'] = $uid;
                }
                $mod_->addAll($family);

                $this->ajaxReturn(array('status' => 1, 'msg' => '注册成功'));
            } else{
                $msg = $mod->getError();
                $this->ajaxReturn(array('status' => 0, 'msg' => $msg));
            }
        }
        $this->display();

    }

    //todo lostpassword
    public function forgot()
    {
        if(IS_AJAX)
        {
            $mobile = I('mobile');
            $email  = I('email');
            //获取用户ID
            $member = M('member')->where(array('mobile' => $mobile))->field('id,username,email')->find();
            if(!$member) $this->ajaxReturn(array('status' => 0, 'msg' => '账户不存在'));
            if($member['email'] != $email) $this->ajaxReturn(array('status' => 0, 'msg' => '邮箱不正确'));
            $secret = md5($mobile.time());
            $tmpl  = '亲爱的用户<span style="color:#E53333;"><strong>'.$member['mobile'].'</strong></span>，您好！<br />';
            $tmpl .= '您可以通过以下链重置密码: <a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].U('passport/reset',array('secret' => $secret)).'"><span style="color:#337FE5;">http://'.$_SERVER['HTTP_HOST'].U('passport/reset',array('secret' => $secret)).'</span></a> 请勿将链接透露给其他人。<br /><br />';
            $tmpl .= '本邮件由系统自动发送，请勿直接回复！<br />';
            $tmpl .= '感谢您的访问，祝您使用愉快';

            if(D('password_secret')->sendMail($email,'密码重置', $tmpl))
            {

                $data = array(
                    'uid' => $member['id'],
                    'secret' => $secret,
                    'addtime' => time(),
                );
                M('password_secret')->add($data);
                $return   = array(
                    'status' => 1,
                    'msg'    => L('发送成功，请前往邮箱查看'),
                );
            } else
            {
                $return   = array(
                    'status' => 0,
                    'msg'    => L('send_wrong'),
                );
            }
            $this->ajaxReturn($return);

        }
        $this->display();
    }

    //密码重置
    public function reset()
    {
        if(IS_AJAX)
        {
            $s = I('post.secret');
            $uid = M('password_secret')->where(array('status' => 0, 'secret' => $s))->order('addtime desc')->getField('uid');
            if(!$uid) $this->ajaxReturn(array('status' => 0, 'msg' => '密钥不正确'));
            $newpass = I('post.password');
            $newpass_confirm = I('post.confirm_pass');
            if(strlen($newpass) < 6)
            {
                $this->ajaxReturn(array('status' => 0, 'msg' => '新密码至少6位'));
            }
            if($newpass != $newpass_confirm)  $this->ajaxReturn(array('status' => 0, 'msg' => '两次输入不一致'));
            $password = md5($newpass);
            M('member')->where(array('id' => $uid))->save(array('password' => $password));
            M('password_secret')->where(array('secret' => $s))->save(array('status' => 1));
            $this->ajaxReturn(array('status' => 1, 'msg' => 密码重置成功));
        }
        $secret = I('get.secret');
        $this->assign('secret', $secret);
        $this->display();
    }


    //todo logout
    public function logout()
    {
        session('current_member', null);
        $this->redirect('index/index');
    }

    public function checkOtherMobile()
    {
        if(IS_AJAX)
        {
            $mobile = I('post.mobile');
            $check = D('member_relation')->checkUnique($mobile);
            $back =($check) ? array('status' => 0) : array('status' => 1);
            $this->ajaxReturn($back);
        }
    }


}